<?php

declare(strict_types=1);

namespace App\CustHub\Mapper;

use App\Common\Constants\StatusConstants;
use App\CustHub\Model\ChApiDoc;
use App\ShortUrl\Model\SuApiDoc;
use Hyperf\Cache\Annotation\Cacheable;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

/**
 * API文档管理Mapper类
 */
class ChApiDocMapper extends AbstractMapper
{
    /**
     * @var ChApiDoc
     */
    public $model;

    public function assignModel()
    {
        $this->model = ChApiDoc::class;
    }

    /**
     * 短链后台文档列表
     * @return array
     */
    #[Cacheable(prefix: 'cust_hub_api_doc',value: 'list', ttl: 3600)]
    public function getApiDocList(): array
    {
        return $this->model::select(['id','article_name'])->where('status',StatusConstants::ENABLE)
            ->where('type',1)
            ->orderBy('sort')
            ->get()->toArray();
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {

        //
        if (isset($params['id']) && filled($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 分类ID
        if (isset($params['cate_id']) && filled($params['cate_id'])) {
            $query->where('cate_id', '=', $params['cate_id']);
        }

        // 文章名称
        if (isset($params['article_name']) && filled($params['article_name'])) {
            $query->where('article_name', 'like', '%'.$params['article_name'].'%');
        }

        // 描述信息
        if (isset($params['desc']) && filled($params['desc'])) {
            $query->where('desc', 'like', '%'.$params['desc'].'%');
        }

        // 作者
        if (isset($params['author']) && filled($params['author'])) {
            $query->where('author', 'like', '%'.$params['author'].'%');
        }

        // 文章内容
        if (isset($params['content']) && filled($params['content'])) {
            $query->where('content', '=', $params['content']);
        }

        // 状态
        if (isset($params['status']) && filled($params['status'])) {
            $query->where('status', '=', $params['status']);
        }

        // 排序
        if (isset($params['sort']) && filled($params['sort'])) {
            $query->where('sort', '=', $params['sort']);
        }

        // 访问量
        if (isset($params['view']) && filled($params['view'])) {
            $query->where('view', '=', $params['view']);
        }

        // 是否推荐
        if (isset($params['is_recommend']) && filled($params['is_recommend'])) {
            $query->where('is_recommend', '=', $params['is_recommend']);
        }

        // 是否热门
        if (isset($params['is_hot']) && filled($params['is_hot'])) {
            $query->where('is_hot', '=', $params['is_hot']);
        }

        // 创建者
        if (isset($params['created_by']) && filled($params['created_by'])) {
            $query->where('created_by', '=', $params['created_by']);
        }

        // 更新者
        if (isset($params['updated_by']) && filled($params['updated_by'])) {
            $query->where('updated_by', '=', $params['updated_by']);
        }

        // 创建时间
        if (isset($params['created_at']) && filled($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 更新时间
        if (isset($params['updated_at']) && filled($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        return $query;
    }
}